package com.sdgs.paas.sdk.core.web.client;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.http.client.SimpleClientHttpRequestFactory;

import com.sdgs.paas.sdk.core.log.PaasLogger;
import com.sdgs.paas.sdk.core.web.PaasResponseErrorHandler;

/**
 * SDK HTTP功能自动配置
 *
 * @author wjj
 */
@Configuration
@ConditionalOnClass({ PaasRestTemplateAuditAspect.class })
@EnableConfigurationProperties({ PaasRestTemplateProperties.class })
public class PaasRestTemplateAutoConfiguration {

	@Bean // 定义REST客户端，RestTemplate实例
	@LoadBalanced // 开启负债均衡的能力
	@Qualifier("internalRestTemplate")
	@Primary
	public PaasRestTemplate internalRestTemplate() {
		PaasRestTemplate restTemplate = new PaasRestTemplate();
		restTemplate.getInterceptors().add(paasHttpRequestInterceptor());
		restTemplate.setErrorHandler(paasResponseErrorHandler());
		PaasLogger.info("PaasRestTemplate创建成功!");
		return restTemplate;
	}

	@Bean // 用来访问外部url
	@Qualifier("externalRestTemplate")
	public PaasRestTemplate externalRestTemplate() {
		PaasRestTemplate restTemplate = new PaasRestTemplate(new SimpleClientHttpRequestFactory(), "external");
		restTemplate.getInterceptors().add(paasHttpRequestInterceptor());
		PaasLogger.info("externalRestTemplate创建成功!");
		return restTemplate;
	}

	@Bean
	@LoadBalanced
	@Qualifier("internalAsyncRestTemplate")
	@Primary
	public PaasAsyncRestTemplate paasAsyncRestTemplate(PaasRestTemplateProperties paasRestTemplateProperties) {
		SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor();
		taskExecutor.setConcurrencyLimit(paasRestTemplateProperties.getConcurrencyLimit());
		PaasAsyncRestTemplate asyncRestTemplate = new PaasAsyncRestTemplate(taskExecutor, "internal");
		asyncRestTemplate.getInterceptors().add(paasHttpRequestInterceptor());
		asyncRestTemplate.setErrorHandler(paasResponseErrorHandler());
		PaasLogger.info("PaasAsyncRestTemplate创建成功!");
		return asyncRestTemplate;
	}

	@Bean
	@Qualifier("externalAsyncRestTemplate")
	public PaasAsyncRestTemplate externalPaasAsyncRestTemplate(PaasRestTemplateProperties paasRestTemplateProperties) {
		SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor();
		taskExecutor.setConcurrencyLimit(paasRestTemplateProperties.getConcurrencyLimit());
		PaasAsyncRestTemplate asyncRestTemplate = new PaasAsyncRestTemplate(taskExecutor, "external");
		asyncRestTemplate.getInterceptors().add(paasHttpRequestInterceptor());
		PaasLogger.info("externalAsyncRestTemplate创建成功!");
		return asyncRestTemplate;
	}

	@Bean
	public IPaasAsyncQueryHelper paasAsyncQueryHelper() {
		IPaasAsyncQueryHelper paasAsyncQueryHelperImpl = new PaasAsyncQueryHelperImpl();
		PaasLogger.info("IPaasAsyncQueryHelper创建成功!");
		return paasAsyncQueryHelperImpl;
	}

	@Bean
	public PaasRestTemplateAuditAspect paasRestTemplateAuditAspect(ApplicationContext applicationContext) {
		PaasRestTemplateAuditAspect paasRestTemplateAuditAspect = new PaasRestTemplateAuditAspect();
		PaasLogger.info("PaasRestTemplateAuditAspect创建成功!");
		return paasRestTemplateAuditAspect;
	}

	@Bean
	public PaasHttpRequestInterceptor paasHttpRequestInterceptor() {
		PaasHttpRequestInterceptor paasHttpRequestInterceptor = new PaasHttpRequestInterceptor();
		PaasLogger.info("PaasHttpRequestInterceptor创建成功!");
		return paasHttpRequestInterceptor;
	}

	@Bean
	public PaasResponseErrorHandler paasResponseErrorHandler() {
		PaasResponseErrorHandler paasResponseErrorHandler = new PaasResponseErrorHandler();
		PaasLogger.info("PaasResponseErrorHandler创建成功!");
		return paasResponseErrorHandler;
	}
}
